package rexsee.fileSelector;

import java.io.File;
import java.util.HashMap;

import rexsee.core.browser.RexseeUrl;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;

public class MediaStoreCache {

	private final HashMap<String, Thumb> THUMBNAILS = new HashMap<String, Thumb>();

	public MediaStoreCache() {
	}

	public static class Thumb {
		public String path;
		public int orientation;
	}

	public Thumb getImageThumbnailFromMediaStore(Context context, String path) {

		try {

			if (THUMBNAILS.containsKey(path)) {
				Thumb thumbnail = THUMBNAILS.get(path);
				File file = new File(thumbnail.path);
				if (file.exists()) {
					return thumbnail;
				} else {
					THUMBNAILS.remove(path);
				}
			}

			ContentResolver contentResolver = context.getContentResolver();
			Cursor cursor = contentResolver.query(Uri.parse(RexseeUrl.URI_MEDIA_IMAGE), new String[]{"_id", "_data", "orientation"}, "_data=?", new String[]{path}, "");

			if (cursor == null) { return null; }
			if (!cursor.moveToFirst()) {
				cursor.close();
				return null;
			}

			int orientation = cursor.getInt(2);

			String id = cursor.getString(0);
			String[] thumbColumns = new String[]{MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID};
			String selection = MediaStore.Images.Thumbnails.IMAGE_ID + "=?";
			String[] selectionArgs = new String[]{id};

			cursor.close();

			Cursor thumbCursor = ((Activity) context).managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);

			if (thumbCursor == null) { return null; }
			if (!thumbCursor.moveToFirst()) {
				//thumbCursor.close();Bug? in note2 it will lead crash and can't catched by try
				return null;
			}

			String thumbnail = thumbCursor.getString(thumbCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));

			//thumbCursor.close(); Bug? in note2 it will lead crash and can't catched by try

			File file = new File(thumbnail);
			if (file.exists()) {
				Thumb thumb = new Thumb();
				thumb.path = file.getAbsolutePath();
				thumb.orientation = orientation;
				THUMBNAILS.put(path, thumb);
				return thumb;
			} else {
				return null;
			}

		} catch (Error err) {
			return null;
		} catch (Exception e) {
			return null;
		}

	}

	public Thumb getVideoThumbnailFromMediaStore(Context context, String path) {

		if (THUMBNAILS.containsKey(path)) {
			Thumb thumbnail = THUMBNAILS.get(path);
			File file = new File(thumbnail.path);
			if (file.exists()) {
				return thumbnail;
			} else {
				THUMBNAILS.remove(path);
			}
		}

		ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(Uri.parse(RexseeUrl.URI_MEDIA_VIDEO), new String[]{"_id", "_data"}, "_data=?", new String[]{path}, "");

		if (cursor == null) { return null; }
		if (!cursor.moveToFirst()) {
			cursor.close();
			return null;
		}

		String id = cursor.getString(0);
		String[] thumbColumns = new String[]{MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID};
		String selection = MediaStore.Video.Thumbnails.VIDEO_ID + "=?";
		String[] selectionArgs = new String[]{id};

		cursor.close();

		Cursor thumbCursor = ((Activity) context).managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);

		if (thumbCursor == null) { return null; }
		if (!thumbCursor.moveToFirst()) {
			thumbCursor.close();
			return null;
		}

		String thumbnail = thumbCursor.getString(thumbCursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA));

		thumbCursor.close();

		File file = new File(thumbnail);
		if (file.exists()) {
			Thumb thumb = new Thumb();
			thumb.path = file.getAbsolutePath();
			thumb.orientation = 0;
			THUMBNAILS.put(path, thumb);
			return thumb;
		} else {
			return null;
		}

	}

	public static Bitmap getVideoThumbnail(String path) {

		Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MICRO_KIND);
		if (bitmap != null) {
			return ThumbnailUtils.extractThumbnail(bitmap, 320, 240, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		} else {
			return null;
		}

	}

}
